home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Library Routines / Text Examples / Matrix Inversion & Determinant < prev    next >
Encoding:
Text File  |  1992-09-03  |  3.8 KB  |  141 lines  |  [TEXT/NCII]

  1. ################
  2. # Matrix routines.
  3. # (Also see the Eigen-Values and Vectors file.
  4. #
  5. #   function Determinant(A)  # Returns the determinant of the matrix A.
  6. #   function Inverse(A) # Returns the inverse of a matrix A.
  7. #   function Identity(N)   # returns NxN identity matrix.
  8. #
  9. #
  10. #  This text file explains and gives examples of
  11. #  some of the routines in the file All Library Routines, which
  12. #  should be Opened before trying any of these examples.
  13. #
  14. #################
  15.  
  16. #########
  17. # The xCOD'sare
  18. xDETERMINANT
  19.   # xCO2 xDETERMINANT(N:extended; theMatrix:RealArray; theDet, Err:Num) ; finds the determinant of a real matrix theMatrix[1…N,1…N] (which is destroyed). N must be < 1000. Err: 0=> success, -1=> N>1000.; an external program.
  20.  
  21.  
  22. xINVERSE
  23.   # xCO2 xINVERSE(N:extended; InputMatrix,OutputMatrix:RealArray; Err:num) ;inverts a real matrix InputMatrix[1…N,1…N] (which is destroyed). N must be <1000. Err: 0=> success, -1=> N>1000, -2=> singular matrix ; an external program.
  24.  
  25.  
  26. #######################
  27. # The interface routines. 
  28.  
  29.  
  30.   function Determinant(A)  # Returns the determinant of the matrix A.
  31. .   var d,n, ans, temp, err
  32. .  #  Input: 
  33. .  #              A[1…n,1…n] = square, real matrix
  34. .  #  Ouput: 
  35. .  #              Determinant = real number
  36. .   begin
  37. .   d=dimension(a)
  38. .   if size(d)<>2 then 
  39. .      print('•• ERROR : not a matrix.')
  40. .   else if d[1]<>d[2] then 
  41. .      print('•• ERROR : not a square matrix.')
  42. .   else
  43. .     begin
  44. .       n = d[1];
  45. .       temp=a
  46. .       xDETERMINANT(n,temp,ans,err)
  47. .       if err<>0 then 
  48. .           Determinant = "ERROR in xDETERMINANT, matrix must be smaller than 1000x1000"
  49. .       else
  50. .           Determinant = ans
  51. .     end
  52. .   end
  53. .   
  54.  
  55.   function Inverse(A) # Returns the inverse of a matrix A.
  56. .   var d,n, ans, temp, err
  57. .  #  Input: 
  58. .  #                 a = square, real matrix
  59. .  #  Output:
  60. .  #                 Inverse = square, real matrix such that Inverse • A = Identity.
  61. .   begin
  62. .   d=dimension(a)
  63. .   if size(d)<>2 then 
  64. .      print('•• ERROR : not a matrix.')
  65. .   else if d[1]<>d[2] then 
  66. .      print('•• ERROR : not a square matrix.')
  67. .   else
  68. .     begin
  69. .       n = d[1];
  70. .       temp=a ; ans=a  # reserve storage space.
  71. .       xINVERSE(n,temp,ans,err)
  72. .       if err=0 then
  73. .         Inverse = ans
  74. .       else if err=-1 then
  75. .         Inverse = "ERROR in xINVERSE, matrix must be smaller than 1000x1000"
  76. .       else 
  77. .          Inverse = "ERROR in xINVERSE, matrix is singular."
  78. .     end
  79. .   end
  80. .   
  81.  
  82.  
  83.   function Identity(N)   # returns NxN identity matrix.
  84. .   var ans,range
  85. . #  Input: positive integer N
  86. . #  Output: Identity = B[1…N,1…N] with B[i,j] = 0 if i<>j;  = 1 if i=j.
  87. .   begin
  88. .     range=1…N;
  89. .     ans[range,range] = 0; #   This sets entire array to 0.
  90. .     for range do ans[range,range]=1;   # While this sets diagonal to 1.
  91. .     Identity=ans
  92. .   end
  93. .   
  94.  
  95.  
  96. ###############################
  97.  
  98. # For example,  if 
  99.  
  100. a = [2,0,0; 1,1,-1; 0,1,0]     # define an array "a".
  101.   a[1…3,1…3] = [[2, 0, 0], 
  102. .                         [1, 1, -1], 
  103. .                         [0, 1, 0]]  .  
  104.  
  105.  
  106. # The determinant of "a" is
  107. Determinant(a)
  108.   -2 
  109.  
  110. # while the inverse of a is
  111. aInv = Inverse(a)
  112.   aInv[1…3,1…3] = [[0.5, 0, 0], 
  113. .                             [0, 0, 1], 
  114. .                             [0.5, -1, 1]]  .  
  115.  
  116. # Using "•" (opt-8 on US keyboard) for matrix multiplication 
  117. # (or implicit multiplication (i.e. a space with no operation) with 
  118. # the Multiply Options (Other menu) set to • for 2-dim arrays)
  119. # we get
  120.  
  121. a • aInv
  122.   [[1, 0, 0], 
  123. .  [0, 1, 0], 
  124. .  [0, 0, 1]]  .  
  125.  
  126. # which is the identity matrix.
  127.  
  128.  
  129.  
  130. ########################
  131. # The Identity function will create an identity matrix.
  132.  
  133. # For example,
  134. Identity(6)
  135.   [[1, 0, 0, 0, 0, 0], 
  136. .  [0, 1, 0, 0, 0, 0], 
  137. .  [0, 0, 1, 0, 0, 0], 
  138. .  [0, 0, 0, 1, 0, 0], 
  139. .  [0, 0, 0, 0, 1, 0], 
  140. .  [0, 0, 0, 0, 0, 1]]  .  
  141.